/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.arg.deploy.webapp.popup.ctrl;

import com.arg.deploy.bean.User;
import com.arg.deploy.common.security.SecurityMD5;
import com.arg.deploy.core.hibernate.UserCore;
import com.arg.deploy.webapp.common.AlertMessages;
import com.arg.deploy.webapp.common.MainPageController;
import static com.arg.deploy.webapp.common.MainPageController.USER_PERMISSION;
import com.arg.deploy.webapp.common.ResourceUtil;
import com.arg.deploy.webapp.common.ValidateManager;
import com.arg.deploy.webapp.common.ValidateUtil;
import java.io.UnsupportedEncodingException;
import org.apache.log4j.Logger;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Button;
import org.zkoss.zul.Textbox;

/**
 *
 * @author sorasaks
 */
public class ChangePasswordCtrl extends MainPageController {

    private static final Logger LOG = Logger.getLogger(ChangePasswordCtrl.class);
    public static final String EXECUTION_USER = "EXECUTION_USER";
    private Textbox oldPassword;
    private Textbox newPassword;
    private Textbox renewPassword;
    private Button btnChangePassword;

    public ChangePasswordCtrl() {
        super();
    }

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp); //To change body of generated methods, choose Tools | Templates.
        initialEvenListener();
    }

    private void initialEvenListener() {
        btnChangePassword.addEventListener(Events.ON_CLICK, new ChangePassword());
    }

    private class ChangePassword implements EventListener {

        @Override
        public void onEvent(Event t) throws Exception {
            if (!validate()) {
                return;
            }

            User user = (User) arg.get(EXECUTION_USER);
            if (user == null) {
                AlertMessages.alertMessage("ผิดพลาด ::: ระบบมีปัญหากรุณาลองอีกครั้งในภายหลัง");
                return;
            }

            SecurityMD5 md5 = new SecurityMD5(oldPassword.getValue());
            User validateUser = UserCore.getUserSignin(user.getUsername(), md5.encoding());
            if (validateUser == null) {
                ValidateManager vm = new ValidateManager();
                vm.add(ValidateUtil.addError(oldPassword, "ผิดพลาด ::: รหัสผ่านเดิมไม่ถูกต้อง"));
                AlertMessages.alertMessage(vm.getMessageTemplate());
                return;
            }

            md5 = new SecurityMD5(newPassword.getValue());
            user.setPassword(md5.encoding());
            Boolean isUpdate = UserCore.update(user);

            if (isUpdate) {
                Executions.getCurrent().getSession().setAttribute(USER_PERMISSION, null);
                Executions.sendRedirect("/");
            } else {
                AlertMessages.alertMessage(ResourceUtil.getLabel("default.alert.message.failed.save", locale));
            }
        }
    }

    private Boolean validate() throws UnsupportedEncodingException {
        ValidateManager vm = new ValidateManager();
        vm.add(ValidateUtil.requireField(oldPassword));
        vm.add(ValidateUtil.requireField(newPassword));
        vm.add(ValidateUtil.requireField(renewPassword));

        if (!vm.getValue()) {
            AlertMessages.alertMessage(vm.getMessageTemplate());
            return Boolean.FALSE;
        }

        if (newPassword.getValue().length() < 7) {
            vm.add(ValidateUtil.addError(newPassword, "ผิดพลาด ::: รหัสผ่านอย่างน้อยต้อง 6 ตัวอักษรขึ้นไป"));
            AlertMessages.alertMessage(vm.getMessageTemplate());
            return Boolean.FALSE;
        }

        if (!newPassword.getValue().equals(renewPassword.getValue())) {
            vm.add(ValidateUtil.addError(newPassword, "ผิดพลาด ::: รหัสผ่านใหม่กรอกไม่ตรงกัน"));
            vm.add(ValidateUtil.addError(renewPassword, "ผิดพลาด ::: รหัสผ่านใหม่กรอกไม่ตรงกัน"));
            AlertMessages.alertMessage(vm.getMessageTemplate());
            return Boolean.FALSE;
        }

        return vm.getValue();
    }
}
